


from sketchz import *

window = WindowAfter((1000, 800))

from box import ana
from itertools import product
from sdl2.ext import FontTTF
Rect = sdl2.SDL_Rect

font = FontTTF("tuffy.ttf", "30px", (0, 0, 0, 255))       # 尼玛这里就是255级的

all_text = sdl2.ext.surface._create_surface((120, 120*100), (255, 255, 255, 255)).contents
ads = Ads100(as_image(window.ctx, all_text))
p1 = [-1, 1, 0, 0, 0, 1,  0, 0]
p2 = [-1, -1, 0, 0, 0, 1, 0, 1]
p3 = [1, -1, 0, 0, 0, 1, 1, 1]
p4 = [1, 1, 0, 0, 0, 1,  1, 0]
arr = np.array([p1, p2, p3, p1, p3, p4], dtype="f4")
ads.vertices = arr[:, :3]
ads.normals = arr[:, 3:6]
ads.texcoords = arr[:, 6:]
ads.u(window)
ads >> window

box = Obj()
box.color = V3(1, 1, 1)
box >> window
box1 = make_block(U3(1, 1, 1))
quad = make_quad('xy', 1, 1)



data = []
now = 0

with open("edit_num_2.txt") as f:
    for line in f:
        code = line.strip()
        shape = ana(code)
        data.append((code, shape))

def calc():
    box.vertices.clear()
    box.normals.clear()

    pos_l = []
    piece_l = []
    sdl2.ext.fill(all_text, (255, 255, 255, 255))

    for (j, i), (code, shape) in zip(product(range(10), range(10)), data[now: now+100]):
        base = U3(i * 7, j * 7, 0)
        pos_l.append(base + U3(0, 0, 4))

        # ~ code = ' '.join(code[i:i+5] for i in range(0, len(code), 5))

        n = j * 10 + i
        piece_l.append((0, n * 1/100, 1/1, 1/100))
        text = font.render_text(code, width=120)
        sdl2.SDL_BlitSurface(text, None, all_text, Rect(0, n * 120, 120, 120))


        for x, y, z in shape:
            box1.t.pos = base + U3(x+1, y+1, z+1)
            box1.as_unit() >> box

        for x, y in product(range(3), range(3)):
            quad.t.pos = base + U3(x, y, -0.5)
            quad.as_unit() >> box

    box.u(window)

    # ~ print(pos_l)
    ads.update100(pos_l, piece_l)
    ads.texture.write(as_image(window.ctx, all_text).read())
    # ~ ads.u(window)

calc()



camera = window.camera
camera.t.pos = U3(0, -5, 1)

walker = Walker3(camera)
walker >> window

class C(Control):
    def down(self, window, e):
        global now
        if e.keysym.sym == sdl2.SDLK_SPACE:
            now = now + 100
            if now > len(data):
                now = 0
            print(now)
            calc()

    def reg_event(self, window):
        window.key_down_callback.append(self.down)


C() >> window

window.mainloop()
